Do not require an API token for `cargo search`
authorJakub Bukaj <jakub@jakub.cc>
Sun, 7 Dec 2014 20:25:32 +0000 (15:25 -0500)
committerJakub Bukaj <jakub@jakub.cc>
Mon, 8 Dec 2014 19:44:24 +0000 (14:44 -0500)
src/registry/lib.rs
tests/test_cargo_search.rs [new file with mode: 0644]
tests/tests.rs

index 952612bcc85e2fdedebc09f6eab3be2df269daa7..4ff2b27308e18cac9802f2f0d37c7aa12381b9fa 100644 (file)
@@ -194,8 +194,8 @@ impl Registry {
                               .header("Accept", "application/json")
                               .content_type("application/json");
 
-        let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice();
         if authorized == Auth::Authorized {
+            let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice();
             req = req.header("Authorization", token);
         }
         match body {
diff --git a/tests/test_cargo_search.rs b/tests/test_cargo_search.rs
new file mode 100644 (file)
index 0000000..83cebb4
--- /dev/null
@@ -0,0 +1,74 @@
+use std::io::{mod, fs, File};
+
+use url::Url;
+
+use cargo::util::{process, ProcessBuilder};
+use support::UPDATING;
+use support::{ResultTest, execs, cargo_dir};
+use support::paths;
+use support::git::repo;
+
+use hamcrest::assert_that;
+
+fn registry_path() -> Path { paths::root().join("registry") }
+fn registry() -> Url { Url::from_file_path(&registry_path()).unwrap() }
+fn api_path() -> Path { paths::root().join("api") }
+fn api() -> Url { Url::from_file_path(&api_path()).unwrap() }
+
+fn setup() {
+    let config = paths::root().join(".cargo/config");
+    fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert();
+    File::create(&config).write_str(format!(r#"
+        [registry]
+            index = "{reg}"
+    "#, reg = registry()).as_slice()).assert();
+    fs::mkdir_recursive(&api_path().join("api/v1"), io::USER_DIR).assert();
+
+    repo(&registry_path())
+        .file("config.json", format!(r#"{{
+            "dl": "{0}",
+            "api": "{0}"
+        }}"#, api()))
+        .build();
+}
+
+fn cargo_process(s: &str) -> ProcessBuilder {
+    process(cargo_dir().join("cargo")).unwrap().arg(s)
+        .cwd(paths::root())
+        .env("HOME", Some(paths::home()))
+}
+
+test!(simple {
+    let crates = api_path().join("api/v1/crates");
+    File::create(&crates).write_str(r#"{
+        "crates": [{
+            "created_at": "2014-11-16T20:17:35Z",
+            "description": "Design by contract style assertions for Rust",
+            "documentation": null,
+            "downloads": 2,
+            "homepage": null,
+            "id": "hoare",
+            "keywords": [],
+            "license": null,
+            "links": {
+                "owners": "/api/v1/crates/hoare/owners",
+                "reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies",
+                "version_downloads": "/api/v1/crates/hoare/downloads",
+                "versions": "/api/v1/crates/hoare/versions"
+            },
+            "max_version": "0.1.1",
+            "name": "hoare",
+            "repository": "https://github.com/nick29581/libhoare",
+            "updated_at": "2014-11-20T21:49:21Z",
+            "versions": null
+        }],
+        "meta": {
+            "total": 1
+        }
+    }"#).assert();
+
+    assert_that(cargo_process("search").arg("postgres"),
+                execs().with_status(0).with_stdout(format!("\
+{updating} registry `[..]`
+hoare (0.1.1)    Design by contract style assertions for Rust", updating = UPDATING)));
+})
index b205bd0e2956c8f7f3b45b80460580bda306be6c..a30e080664a2a5c0016e130847bed15b697fe01e 100644 (file)
@@ -27,27 +27,28 @@ macro_rules! test(
 
 mod test_cargo;
 mod test_cargo_bench;
+mod test_cargo_build_auth;
 mod test_cargo_build_lib;
 mod test_cargo_clean;
 mod test_cargo_compile;
 mod test_cargo_compile_custom_build;
-mod test_cargo_compile_old_custom_build;
 mod test_cargo_compile_git_deps;
+mod test_cargo_compile_old_custom_build;
 mod test_cargo_compile_path_deps;
-mod test_cargo_test;
-mod test_shell;
-mod test_cargo_cross_compile;
-mod test_cargo_run;
-mod test_cargo_version;
-mod test_cargo_new;
 mod test_cargo_compile_plugins;
+mod test_cargo_cross_compile;
 mod test_cargo_doc;
 mod test_cargo_features;
+mod test_cargo_fetch;
 mod test_cargo_freshness;
 mod test_cargo_generate_lockfile;
-mod test_cargo_profiles;
+mod test_cargo_new;
 mod test_cargo_package;
-mod test_cargo_build_auth;
-mod test_cargo_registry;
+mod test_cargo_profiles;
 mod test_cargo_publish;
-mod test_cargo_fetch;
+mod test_cargo_registry;
+mod test_cargo_run;
+mod test_cargo_search;
+mod test_cargo_test;
+mod test_cargo_version;
+mod test_shell;